/**
 * Created by a1 on 16/4/29.
 */

var url = "http://api.1-blog.com/biz/bizserver/xiaohua/list.do"
var P_MaxId = "maxXhid"
var P_MinId = "minXhid"
var P_Size = "size"
var size = 20

var kId = "xhid"
var kAuthor = "author"
var kContent = "content"
var kImageUrl = "picUrl"

var maxId = 0
var minId = 0

var body
var btnBack
var nodeTitle
var containerList
var nodeList
var containerDetail
var nodeDetail

var Show = "show"
var AnimIn = "animIn"
var AnimOut = "animOut"

var scrollTop

$(function() {
    //alert("init")
    init()
    loadList()
})

function init() {
    //加载Utils.js
    var node = $("<script type='text/javascript' src='../../Utils.js'></script>")
    $("head").append(node)

    nodeTitle = $("#title")
    nodeTitle.html("新闻列表")

    containerList = $("#containerList")
    containerList.addClass(Show)
    nodeList = $("#list")

    containerDetail = $("#containerDetail")
    nodeDetail = $("#detail")

    btnBack =  $("#btnBack")
    btnBack.click(function() {
        if(btnBack.hasClass(Show)) {
            hideDetail()
        }
    })
}

function loadList() {
    var params = {}
    params[P_MaxId] = maxId
    params[P_MinId] = minId
    params[P_Size] = size
    //alert("params = " + JSON.stringify(params))
    $.getJSON(url, params, function(data) {
        var length = data["detail"].length
        //alert("data = " + length)
        for(var  i = 0; i < length; ++i) {
            addItem(nodeList, i, data["detail"][i])
        }
    })
}

function addItem(parent, index, data) {
    var id = data[kId]
    if(maxId == 0 || id > maxId) {
        maxId = id
    }

    if(minId == 0 || id < minId) {
        minId = id
    }
    //alert(index + " = " + JSON.stringify(data))
    var item = $("<div class='item'></div>")
    var content = $("<div class='content'>"+data[kContent]+"</div>")
    var imageUrl = data[kImageUrl]
    var hasImage = trim(imageUrl)
    if(hasImage) {
        var imageView = $("<img class='imageSmall' src='"+imageUrl+"'/>")
        item.append(imageView)
        //重置文本与item的size
        content.attr("size", "")
        item.attr("size", "")
    }
    item.append(content)
    parent.append(item)

    item.click(function() {
        loadDetail(data)
    })
}

function loadDetail(data) {
    //alert("load Detail")
    //sessionStorage.detaildata = data
    //var body = $("body")
    scrollTop = body.scrollTop()

    btnBack.addClass(Show)

    containerDetail.addClass(Show)
    containerDetail.removeClass(AnimOut)
    containerDetail.addClass(AnimIn)

    setTimeout(function() {
        containerList.removeClass(Show)
    }, 400)

    var htmlStr = data[kContent]
    nodeDetail.html(htmlStr)

    var nodeImage = $("#imageDetail")
    nodeImage.attr("src", data[kImageUrl])
    body.scrollTop(0)
}

function hideDetail() {
    btnBack.removeClass(Show)
    containerList.addClass(Show)

    containerDetail.removeClass(AnimIn)
    containerDetail.addClass(AnimOut)

    setTimeout(function() {
        body.scrollTop(scrollTop)
        containerDetail.removeClass(Show)
    }, 400)
}
